﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageSaccCalculator.aspx.cs" Inherits="InterestCalculation.Website_Project.Page.PageSaccCalculator" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Simple Interest Calculator</title>
    <script src="../../Scripts/jacs.js"></script>
    <script src="../../Scripts/jquery-1.7.1.min.js"></script>
    <link href="../../Scripts/easyui.css" rel="stylesheet" />
    <link href="../../Scripts/themes/icon.css" rel="stylesheet" />
    <script src="../../Scripts/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        function Calculate() {
            var arr = {
                Frequency: $("#ddlFrequency").val(),
                Principal: $("#txtPrincipal").val(),
                MonthlyFee: $("#txtMonthlyFee").val(),
                LoanDate: $("#txtLoanDate").val(),
                FirstRepaymentDate: $("#txtFirstRepayment").val(),
                LoanDuration: $("#txtLoanDuration").val(),
                Estabalishment: $("#txtEstablistmentCharges").val(),
            }
            $.ajax({
                url: "/Website Project/Web Method/WebMethodSaacCalculator.ashx?method=SaacSchedule&insertstring=" +JSON.stringify(arr),
                contentType: "application/json; charset=utf-8",
                type: "POST",
                datatype: "JSON",
                data: {} ,
                success: function (data) {
                    alert(data);
                },
                error: function (err) {
                    message(err.responseText);
                }
            })
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>

                <tr>
                    <td>Select Frequency</td>
                    <td>
                        <asp:DropDownList ID="ddlFrequency" runat="server">
                            <asp:ListItem Text="Weekly" Value="0"></asp:ListItem>
                            <asp:ListItem Text="Fortnighty" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Monthly" Value="2"></asp:ListItem>
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td>Principal</td>
                    <td>
                        <asp:TextBox ID="txtPrincipal" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Monthly Fee</td>
                    <td>
                        <asp:TextBox ID="txtMonthlyFee" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Loan Date</td>
                    <td>
                        <asp:TextBox ID="txtLoanDate" runat="server"></asp:TextBox>&nbsp;
                    <input name="btnLoanDate" value="+" id="btnLoanDate" type="button" />
                    </td>
                </tr>
                <tr>
                    <td>First Repayment Date</td>
                    <td>
                        <asp:TextBox ID="txtFirstRepayment" runat="server"></asp:TextBox>
                        <input name="btnLoanDate" value="+" id="btnFirstRepayment" type="button" />
                    </td>
                </tr>
                <tr>
                    <td>Loan Duration (in week)</td>
                    <td>
                        <asp:TextBox ID="txtLoanDuration" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Establistment charges</td>
                    <td>
                        <asp:TextBox ID="txtEstablistmentCharges" runat="server"></asp:TextBox>
                    </td>
                </tr>
            </table>
            <asp:Button ID="btnCalculate" runat="server" OnClientClick="return Calculate();" Text="Calculate" />
        

            <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" width="80%">
                <tr>
                    <th colspan="2">Workings</th>
                </tr>
                <tr>
                    <td>No. of Repayments</td>
                    <td>
                        <div id="NoOfRepayments">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Outer Limit For repayment</td>
                    <td>
                        <div id="OuterLimitForRepayment">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Last Replacement Date</td>
                    <td>
                        <div id="LastRepaymentDate">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Maximum Monthly Fee</td>
                    <td>
                        <div id="MaximumMonthlyFee">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Repayment Amount</td>
                    <td>
                        <div id="RepaymentAmount">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Establistment charges</td>
                    <td>
                        <div id="EstablistmentCharges">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Actual Loan Duration</td>
                    <td>
                        <div id="ActualLoanDuration">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>No. of Months</td>
                    <td>
                        <div id="NoOfMonths">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Days in the Period</td>
                    <td>
                        <div id="DaysInThePeriod">
                        </div>
                    </td>
                </tr>
            </table>

            <table>
                <tr>
                    <td></td>
                    <td>No. of Repayments</td>
                    <td>Last Repayment Date</td>
                </tr>
                <tr>
                    <td>Monthly</td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Fortnightly</td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Weekly</td>
                    <td></td>
                    <td></td>
                </tr>
            </table>
            <br />
            <br />




           <%-- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="74px">
            <Columns>
                <asp:BoundField HeaderText="Pay#" />
                <asp:BoundField HeaderText="Date" />
                <asp:BoundField HeaderText="Repayment" />
                <asp:BoundField HeaderText="Principal" />
                <asp:BoundField HeaderText="Interest" />
                <asp:BoundField HeaderText="Balance" />
            </Columns>
        </asp:GridView>--%>
        </div>
        
    </form>
</body>
</html>

<script type="text/javascript">
    function calculateLoan(IsUpdate) {
        if (validate() == "") {
            if (!(Is_Date($("#<%=txtFirstRepayment.ClientID%>").val()))) {
                message("Please enter valid repayment date");
                return false;
            }
            if (!(Is_Date($("#<%=txtLoanDate.ClientID%>").val()))) {
                message("Please enter valid loan date")
                return false;
            }
            clear();
            loading();
            if (IsUpdate == "false") {
                $("#btnUpdate").css("display", "none")
            }
            var arr = {
                "app_id": "12345678",
                "frequency": "" + $("#<%=ddlFrequency.ClientID%>").val() + "",
                "principal": "" + $("#<%=txtPrincipal.ClientID%>").val() + "",
                "LoanDuration": "" + $("#<%=txtLoanDuration.ClientID%>").val() + "",
                "MonthlyFee": "" + $("#<%=txtMonthlyFee.ClientID%>").val() + "",
                "FirstRepaymentDate": "" + $("#<%=txtFirstRepayment.ClientID%>").val() + "",
                "LoanDate": "" + $("#<%=txtLoanDate.ClientID%>").val() + "",
                "EstabalishmentFee": "" + $("#<%=txtEstablistmentCharges.ClientID%>").val() + "",
                "MonthlyFee": "" + $("#<%=txtMonthlyFee.ClientID%>").val() 
                
            };
            $.ajax({
                url: "/Website Project/Web Method/WebMethodLoanCalculator.ashx?method=calculateLoan",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                datatype: "JSON",
                data: JSON.stringify(arr),
                success: function (data) {
                    writeHTML(data, IsUpdate);
                    loading();
                },
                error: function (err) {
                    message(err.responseText);
                    loading();
                }
            })
        } else {
            message(validate());
            
        }
        return false;
    }
    function message(msg) {
        $.messager.show({
            title: 'Simple Interest Calculator',
            msg: msg,
            showType: 'show',
            timeout: 0,
            style: {
                right: '',
                top: document.body.scrollTop + document.documentElement.scrollTop,
                bottom: '',
            }
        });
    }
    function getValue(eleId, dtype) {
        var val;
        var type = "";
        if (typeof (dtype) != undefined) {
            type = dtype;
        }
        if (type == "d") {
            val = 0;
        } else {
            val = "";
        }
        if (document.getElementById(eleId)) {
            if (type == "d") {
                if ((!(isNaN($("#" + eleId).val()))) && $("#" + eleId).val() != "") {
                    val = $("#" + eleId).val();
                } else {
                    val = 0;
                }
            } else {
                val = $("#" + eleId).val();
            }
        }
        return val;
    }

    function validate() {

    }
    function writeHTML(data, IsUpdate) {
        
       
}
function getArrValue(name) {
    var arr = new Array();
    $("input[name='" + name + "']").each(function () {
        arr.push($(this).val());
    })
    if (arr.length == 0) {
        arr.push(0);
    }
    return arr;
}
function Display(id) {

}
function convertToPercentage(val) {
    var _tempval = 0;
    if (!(isNaN(val))) {
        _tempval = parseFloat(val) * 100;
    }
    return _tempval;
}
function convertToDate(jsonDate) {
    var date = "";
    if (jsonDate != "" && jsonDate != null) {
        var value = new Date(parseInt(jsonDate.substr(6)));
        date = value.getDate() + "/" + placeZero((value.getMonth() + 1)) + "/" + value.getFullYear();
    }
    return date;
}
function placeZero(month) {
    if (month.toString().length == 1) {
        return month = "0" + month;
    } else {
        return month;
    }
}
function roundUp(val, decimalplace) {
    var _mulVar = 1;
    switch (decimalplace) {
        case 1:
            _mulVar = 10;
            break;
        case 2:
            _mulVar = 100;
            break;
        case 3:
            _mulVar = 1000;
            break;
        case 4:
            _mulVar = 10000;
            break;
        case 5:
            _mulVar = 100000;
            break;
        case 6:
            _mulVar = 1000000;
            break;
        case 7:
            _mulVar = 10000000;
            break;
        case 8:
            _mulVar = 100000000;
            break;

    }
    var _roundValue = 0;
    if (!(isNaN(val))) {
        _roundValue = Math.round(parseFloat(val) * parseFloat(_mulVar)) / parseInt(_mulVar);
    }
    return _roundValue;
}
function clear() {
    $("#divNoOfRepayments").html("");
    $("#divOuterLimitForRepayment").html("");
    $("#divLastRepaymentDate").html("");
    $("#divMaximumMonthlyFee").html("");
    $("#divRepaymentAmount").html("");
    $("#divEstablistmentCharges").html("");
    $("#divNoOfMonths").html("");
    $("#divDaysInThePeriod").html("");

}
function checkValidInput(ele) {
    if (ele.value.trim() != "") {
        if (isNaN(ele.value)) {
            ele.value = 0;
        }
    } else {
        ele.value = 0;
    }
}
function loading() {
    if ($("#loading").html() == "") {
        $("#loading").html("<img src='/Images/load.gif' alt='Loading...' />");
    } else {
        $("#loading").html("");
    }
}
function Is_Date(dt) {
    var tdate = new Date();
    splitDate = dt.split("/");
    refDate = new Date(splitDate[1] + "/" + splitDate[0] + "/" + splitDate[2]);
    if (splitDate[1] < 1 || splitDate[1] > 12 || refDate.getDate() != splitDate[0] || splitDate[2].length != 4) {
        return false;
    } else {
        if (splitDate[2] < (tdate.getFullYear() - 100) || splitDate[2] > (tdate.getFullYear() + 100)) {
            return false;
        } else {
            return true;
        }
    }
}
</script>
